package hu.kisprof.quiz.view.panels;

import hu.kisprof.quiz.model.bean.GameQuestion;
import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;

public class QuizGamePanel extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7913989655099105232L;

	private QuizGUI gui;
	private JTextArea  questionTA = new JTextArea(Labels.question);
	private JButton ans1 = new JButton(Labels.answer+" 1");
	private JButton ans2 = new JButton(Labels.answer+" 2");
	private JButton ans3 = new JButton(Labels.answer+" 3");
	private JButton ans4 = new JButton(Labels.answer+" 4");
	private JProgressBar progressBar= new JProgressBar(0,30);
	private ProgressBarTimer timeToAnswer;
	private String correctAns = "";
	private ArrayList<JLabel> p1ans = new ArrayList<JLabel>();
	private ArrayList<JLabel> p2ans = new ArrayList<JLabel>();
	private JPanel p1AnsPanel = new JPanel(new GridLayout(10,2));
	private JPanel p2AnsPanel = new JPanel(new GridLayout(10,2));
	private JPanel p1Panel;
	private JPanel p2Panel;
	private PlayerPanel pPanel;
	
	
	
	private volatile String answered =null;
	private int round = 0;
	private int numberOfAnswers = 0;
	public QuizGamePanel(QuizGUI gui){
		super(new BorderLayout());
		this.gui=gui;
		JPanel questionPart = new JPanel(new BorderLayout());
			JPanel answerButtons = new JPanel(new GridLayout(2,2));
			answerButtons.add(ans1);
			answerButtons.add(ans2);
			answerButtons.add(ans3);
			answerButtons.add(ans4);
			ans1.addActionListener(this);
			ans2.addActionListener(this);
			ans3.addActionListener(this);
			ans4.addActionListener(this);
			questionPart.add(progressBar, BorderLayout.NORTH);
			questionPart.add(questionTA, BorderLayout.CENTER);
			questionPart.add(answerButtons, BorderLayout.SOUTH);
		
			
			
		
			for(int i =1; i<= 10;i++ ){
				p1AnsPanel.add(new JLabel(i+". "+Labels.question+":"));
				p1ans.add(new JLabel(Labels.unknown));
				p1AnsPanel.add(p1ans.get(i-1));
				p2AnsPanel.add(new JLabel(i+". "+Labels.question+":"));
				p2ans.add(new JLabel(Labels.unknown));
				p2AnsPanel.add(p2ans.get(i-1));
			}
			
			
			
		JPanel statsPart = new JPanel(new GridLayout(1,2));
			p1Panel = new JPanel(new BorderLayout());
			p1Panel.add(new JLabel(Labels.you+":"),BorderLayout.NORTH);
			p1Panel.add(p1AnsPanel);
			statsPart.add(p1Panel);
			
			p2Panel = new JPanel(new BorderLayout());
			p2Panel.add(new JLabel(Labels.enemy+":"),BorderLayout.NORTH);
			p2Panel.add(p2AnsPanel);
			statsPart.add(p2Panel);
		pPanel=new PlayerPanel(gui,gui.getControl().getPlayer());
		add(pPanel, BorderLayout.NORTH);
		add(questionPart,BorderLayout.CENTER);
		add(statsPart,BorderLayout.SOUTH);
		progressBar.setStringPainted(true);
		progressBar.setString(progressBar.getMaximum()+"s left.");
	}
	
	public void newQuestion(GameQuestion q){
		progressBar.setValue(0);
		ans1.setEnabled(true);
		ans2.setEnabled(true);
		ans3.setEnabled(true);
		ans4.setEnabled(true);
		round++;
		answered=null;
		if(timeToAnswer!=null){
			timeToAnswer.stopTheBar();
		}
		
		
		correctAns = q.getgAnswer();
		ArrayList<String> ans = q.getAnswers();
		this.questionTA.setText(q.getQuestion());
		ans1.setText(ans.get(0));
		ans2.setText(ans.get(1));
		ans3.setText(ans.get(2));
		ans4.setText(ans.get(3));
		this.revalidate();
		timeToAnswer =new ProgressBarTimer();
		timeToAnswer.start();
		while(answered == null){
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(answered==correctAns){
			JOptionPane.showMessageDialog(this, Labels.good, Labels.good, JOptionPane.INFORMATION_MESSAGE);
			p1ans.get(round-1).setText(Labels.good);
			p1ans.get(round-1).setOpaque(true);
			p1ans.get(round-1).setBackground(Color.GREEN);
			
		}else{
			JOptionPane.showMessageDialog(this, Labels.wrong, Labels.wrong, JOptionPane.INFORMATION_MESSAGE);
			p1ans.get(round-1).setText(Labels.wrong);
			p1ans.get(round-1).setOpaque(true);
			p1ans.get(round-1).setBackground(Color.RED);
		}
		gui.getControl().playerReady(answered==correctAns?true:false);
		pPanel.refreshPoint();
		if(++numberOfAnswers==20){
			gui.setActualContent(new QuizEndGamePanel(gui, p1Panel, p2Panel));
		}
	}
		
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==ans1){
			if(answered==null){
				answered = ans1.getText();
				ans2.setEnabled(false);
				ans3.setEnabled(false);
				ans4.setEnabled(false);
			}
		}
		if(e.getSource()==ans2){
			if(answered==null){
				answered = ans2.getText();
				ans1.setEnabled(false);
				ans3.setEnabled(false);
				ans4.setEnabled(false);
			}
		}
		if(e.getSource()==ans3){
			if(answered==null){
				answered = ans3.getText();
				ans1.setEnabled(false);
				ans2.setEnabled(false);
				ans4.setEnabled(false);
			}
		}
		if(e.getSource()==ans4){
			if(answered==null){
				answered = ans4.getText();
				ans1.setEnabled(false);
				ans2.setEnabled(false);
				ans3.setEnabled(false);
			}
		}
		
	}

	
	private class ProgressBarTimer extends Thread{
		private boolean running;

		public void stopTheBar(){
			running = false;
		}
		
		@Override
		public void run() {
			running = true;
			int i =0;
			while(i<=progressBar.getMaximum()&&running == true){
				try {
					progressBar.setString((progressBar.getMaximum()-i)+"s left.");
					progressBar.setValue(i++);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
				if(answered == null && running == true){
					answered ="";
				}
		}
	}


	public void newAnswer(boolean answer) {
		p2ans.get(round-1).setOpaque(true);
		p2ans.get(round-1).setBackground(answer?Color.GREEN:Color.RED);
		p2ans.get(round-1).setText(answer?Labels.good:Labels.wrong);
		this.validate();
		if(++numberOfAnswers==20){
			gui.setActualContent(new QuizEndGamePanel(gui, p1Panel, p2Panel));
		}
	}
	

}
